﻿using FAP.GetComment.DTO;
using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using FAP.DataAccess.BLL;

namespace FAP.GetComment.GUI
{
    public partial class frmGetComment : Form
    {
        List<News> lstNews;
        private List<XPath> GetXPath()
        {
            List<XPath> xPaths = new List<XPath>();
            XPath xpath = new XPath();

            xpath.Id = 0;
            xpath.Domain = "Choose";
            xPaths.Add(xpath);

            xpath = new XPath();
            xpath.Id = 1;
            xpath.Domain = "http://forum.zing.vn/";
            xpath.Url = "http://forum.zing.vn/fanfiction/12-chom-sao-noi-ay-anh-co-gap-lai-em/t1308530.html";
            xpath.Root = "//*[@id=\"postlist\"]/ol/li";
            xpath.Date = "div[1]/div[1]/span[1]";
            xpath.Authors = "div[1]/div[2]/div[1]/div[1]/div[1]/a[1]/strong[1]";
            xpath.Title = "div[2]/div[1]/h2[1]";
            xpath.Content = "div[2]/div[1]/div[1]/div[1]/blockquote[1]";
            xPaths.Add(xpath);

            xpath = new XPath();
            xpath.Id = 2;
            xpath.Domain = "http://fap.tonghophoanhao.com";
            xpath.Root = "//*[@id=\"postlist\"]/ol/li";
            xpath.Date = "div[1]/span[1]/span[1]";
            xpath.Url = "http://fap.tonghophoanhao.com/showthread.php?9-Bai-post-1";
            xpath.Authors = "div[2]/div[1]/div[1]/div[1]/a[1]/strong[1]";
            xpath.Title = "div[2]/div[2]/div[1]/h2[1]";
            xpath.Content = "div[2]/div[2]/div[1]/div[1]/div[1]/blockquote[1]";
            xPaths.Add(xpath);

            xpath = new XPath();
            xpath.Id = 3;
            xpath.Domain = "http://www.webtretho.com/";
            xpath.Root = "//*[@id=\"postlist\"]/ol/li";
            xpath.Date = "div[1]/span[1]/span[1]";
            xpath.Url = "http://www.webtretho.com/forum/f187/hay-viet-nhung-gi-ban-dang-nghi-tang-3-a-1759850/";
            xpath.Authors = "div[1]/div[2]/div[1]/div[1]/div[1]/a[1]/strong[1]";
            xpath.Title = "div[2]/div[1]/h2[1]";
            xpath.Content = "div[2]/div[1]/div[1]/div[1]/blockquote[1]";
            xPaths.Add(xpath);

            xpath = new XPath();
            xpath.Id = 4;
            xpath.Domain = "http://www.vn-zoom.com";
            xpath.Root = "//*[@id=\"postlist\"]/div/ol/li";
            xpath.Date = "div[1]/div[1]/span[1]/span[1]";
            xpath.Url = "http://www.vn-zoom.com/f2/thong-bao-ve-trang-download-cua-vn-zoom-2407358.html";
            xpath.Authors = "div[1]/div[2]/div[1]/div[1]/div[1]/a[1]/strong[1]";
            xpath.Title = "div[2]/div[1]/h2[1]";
            xpath.Content = "div[2]/div[1]/div[1]/div[1]/blockquote[1]";
            xPaths.Add(xpath);

            xpath = new XPath();
            xpath.Id = 5;
            xpath.Domain = "http://www.diendan.eva.vn";
            xpath.Root = "//*[@id=\"posts\"]/div";
            xpath.Date = "div/div/div/table[1]/tr[1]/td[1]/div[2]";
            xpath.Url = "http://www.diendan.eva.vn/huong-dan-mua-va-ban-hang-trong-sieu-thi-ishop-t75939.html";
            xpath.Authors = "div/div/div/table[1]/tr[2]/td[1]/table[1]/tr[1]/td[2]/div[1]/a[1]/font[1]/b[1]";
            xpath.Title = "div/div/div/table[1]/tr[3]/td[1]/div[1]/strong[1]";
            xpath.Content = "div/div/div/table[1]/tr[3]/td[1]/div[2]";
            xPaths.Add(xpath);

            xpath = new XPath();
            xpath.Id = 6;
            xpath.Domain = "http://raovat.123mua.vn/";
            xpath.Root = "//*[@id=\"content\"]/div[2]";
            xpath.Date = "div[3]/div[2]/div[1]/p[1]/span[2]";
            xpath.Url = "http://raovat.123mua.vn/TP-HCM/Du-hoc-thuy-sy-chuong-trinh-vua-hoc-vua-lam-323089.html";
            xpath.Authors = "div[3]/div[2]/div[2]/span[2]/span[2]/a[1]";
            xpath.Title = "div[3]/div[1]/h1[1]";
            xpath.Content = "div[3]/div[5]";
            xPaths.Add(xpath);

            return xPaths;
        }

        private void SaveData(List<News> news)
        {

            foreach (var item in news)
            {
                comment cm = new comment();
                FAP.DataAccess.BLL.comment.commentInsertRow(0, 1, item.Url, item.Authors, item.Title, item.Content, DateTime.Now, false);

            }
        }

        public frmGetComment()
        {

            InitializeComponent();
            cmbForum.DataSource = GetXPath();
            cmbForum.DisplayMember = "Domain";
            cmbForum.ValueMember = "Id";

        }

        /// <summary>
        /// Onclick get comment 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnGetComment_Click(object sender, EventArgs e)
        {
            lblProcess.Text = "0%";
            XPath xpath = new XPath();
            int index = cmbForum.SelectedIndex;

            xpath.Url = txtUrl.Text;
            xpath.Root = txtXPathRoot.Text;
            xpath.Title = txtXPathTitle.Text;
            xpath.Authors = txtXPathAuthors.Text;
            xpath.Content = txtXPathContent.Text;
            xpath.Date = txtXPathDate.Text;
            if (string.IsNullOrEmpty(xpath.Url) || string.IsNullOrEmpty(xpath.Root)
                || string.IsNullOrEmpty(xpath.Authors) || string.IsNullOrEmpty(xpath.Title)
                || string.IsNullOrEmpty(xpath.Content))
            {
                MessageBox.Show("[XPath field] is required!", "Error");
                return;
            }
            xpath.Date = GetXPath()[index].Date;
            if (!string.IsNullOrEmpty(txtUrl.Text))
            {
                xpath.Url = txtUrl.Text;
            }

            lstNews = new List<News>();
            GetCommentByWebsiteVB(xpath);
            LoadData();
        }

        public void GetCommentByWebsite(XPath xpath)
        {
            lstNews = new List<News>();
            var site = new HtmlWeb();
            var doc = site.Load(xpath.Url);
            HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes(xpath.Root);
            if (nodes != null)
            {
                News model;
                foreach (var node in nodes)
                {
                    HtmlNodeCollection nodeList = node.SelectNodes("ol/li");
                    foreach (var item in nodeList)
                    {
                        model = new News();
                        model.Url = xpath.Url;
                        model.Id = xpath.Id;
                        var tempAuthors = item.SelectNodes(xpath.Authors);
                        if (tempAuthors != null && tempAuthors.Count > 0)
                        {
                            model.Authors = tempAuthors[0].InnerText.Trim();
                        }

                        var tempDate = item.SelectNodes(xpath.Date);
                        if (tempDate != null && tempDate.Count > 0)
                        {
                            model.Date = tempDate[0].InnerText.Trim();
                        }

                        var tempTitle = item.SelectNodes(xpath.Title);
                        if (tempTitle != null && tempTitle.Count > 0)
                        {
                            model.Title = tempTitle[0].InnerText.Trim();
                        }
                        var tempContent = item.SelectNodes(xpath.Content);
                        if (tempContent != null && tempContent.Count > 0)
                        {
                            model.Content = tempContent[0].InnerText.Trim();
                        }
                        if (tempAuthors == null && tempContent == null)
                        {
                            continue;
                        }
                        lstNews.Add(model);
                    }
                }
            }
        }

        public void GetCommentByWebsiteVB(XPath xpath)
        {
            lstNews = new List<News>();
            var site = new HtmlWeb();
            var doc = site.Load(xpath.Url);
            HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes(xpath.Root);
            if (nodes != null)
            {
                News model;

                foreach (var item in nodes)
                {
                    model = new News();
                    model.Url = xpath.Url;
                    var tempAuthors = item.SelectNodes(xpath.Authors);
                    if (tempAuthors != null && tempAuthors.Count > 0)
                    {
                        model.Authors = tempAuthors[0].InnerText.Trim();
                    }

                    var tempDate = item.SelectNodes(xpath.Date);
                    if (tempDate != null && tempDate.Count > 0)
                    {
                        model.Date = tempDate[0].InnerText.Trim();
                    }

                    var tempTitle = item.SelectNodes(xpath.Title);
                    if (tempTitle != null && tempTitle.Count > 0)
                    {
                        model.Title = tempTitle[0].InnerText.Trim();
                    }
                    var tempContent = item.SelectNodes(xpath.Content);
                    if (tempContent != null && tempContent.Count > 0)
                    {
                        model.Content = tempContent[0].InnerText.Trim();
                    }
                    if (tempAuthors == null && tempContent == null)
                    {
                        continue;
                    }
                    lstNews.Add(model);
                }

            }
        }

        private void LoadData()
        {
            int iCount = lstNews.Count;
            lvNews.Items.Clear();
            lstNews = lstNews.OrderBy(m => m.Authors).ToList();
            pb_TaskProccess.Minimum = 0;
            pb_TaskProccess.Maximum = lstNews.Count;
            pb_TaskProccess.Value = 0;
            pb_TaskProccess.Step = 1;

            foreach (var item in lstNews)
            {
                ListViewItem lvi = new ListViewItem();
                lvi.Text = item.Authors;
                lvi.SubItems.Add(item.Url);
                lvi.SubItems.Add(item.Title);
                lvi.SubItems.Add(item.Content);
                lvi.SubItems.Add(item.Date);
                lvNews.Items.Add(lvi);
                pb_TaskProccess.PerformStep();
                lblProcess.Text = pb_TaskProccess.Value + "/" + iCount;
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cmbForum_SelectedIndexChanged(object sender, EventArgs e)
        {
            pb_TaskProccess.Value = 0;
            lblProcess.Text = pb_TaskProccess.Value + "/0";
            lvNews.Items.Clear();
            XPath xpath = (XPath)cmbForum.SelectedItem;
            txtUrl.Text = xpath.Url;
            txtXPathRoot.Text = xpath.Root;
            txtXPathTitle.Text = xpath.Title;
            txtXPathAuthors.Text = xpath.Authors;
            txtXPathContent.Text = xpath.Content;
            txtXPathDate.Text = xpath.Date;
        }


    }
}
